package com.geping.etl.common.service.impl;

import com.geping.etl.common.entity.Sys_User;
import com.geping.etl.common.repository.Sys_UserRepository;
import com.geping.etl.common.service.SysLockUserService;
import com.geping.etl.common.util.Constants;
import com.geping.etl.utils.EnvUitl;
import com.geping.etl.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysLockUserServiceImpl implements SysLockUserService {


    @Override
    public void lockUserWhenNotLoggedIn30Day() {
        String lockUserJobSwitch = EnvUitl.props.getProperty(Constants.LOCK_USER_JOB_SWITCH);
        log.info("[lockUserWhenNotLoggedIn30Day]开始执行,lockUserJobSwitch :{}", lockUserJobSwitch);
        if (StringUtils.isBlank(lockUserJobSwitch) || !(lockUserJobSwitch.equals(Constants.ON))) {
            log.info("任务未开启");
            return;
        }
        log.info("任务开启,开始执行");
        String admin = getAdmin();
        try {
            Sys_UserRepository ror = (Sys_UserRepository) SpringContextUtil.getBean("sys_UserRepository");
            List<Sys_User> list = ror.findAllSysUser();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Sys_User user : list) {
                if (StringUtils.isBlank(user.getLastLoginDate()) || user.getLoginId().equals(admin)) {
                    continue;
                }
                int days = (int) ((new Date().getTime() - sdf.parse(user.getLastLoginDate()).getTime()) / (1000 * 60 * 60 * 24));
                int lockUserDays = getLockUserJobDays();
                if (days >= lockUserDays) {
                    ror.agreenLock(user.getId(), "Y");
                }
            }
        } catch (Exception e) {
            log.error("lockUserWhenNotLoggedIn30Day error->", e);
        }
    }

    /**
     * 获取管理员账号
     *
     * @return
     */
    private String getAdmin() {
        String admin = EnvUitl.props.getProperty(Constants.ACCOUNT_ADMIN_INIT);
        if (StringUtils.isBlank(admin)) {
            admin = Constants.ACCOUNT_ADMIN;
        }
        return admin;
    }

    /**
     * 判断字符串是否是数字
     * @param str
     * @return
     */
    private boolean isNumeric(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        return str.chars().allMatch(Character::isDigit);
    }

    /**
     *  获取超时未登录锁定天数
     * @return
     */
    private int getLockUserJobDays() {
        String lockUserJobDays = EnvUitl.props.getProperty(Constants.LOCK_USER_JOB_DAYS);
        int lockUserDays = 0;
        // 判断是否配置超过一定天数未登录锁定，如果没配置或配置不正确使用默认配置
        try {
            lockUserDays = isNumeric(lockUserJobDays) ? Integer.valueOf(lockUserJobDays) : Constants.DEFAULT_LOCK_USER_DAYS;
            lockUserDays = lockUserDays > 0 ? lockUserDays : Constants.DEFAULT_LOCK_USER_DAYS;
        } catch (Exception e) {
            lockUserDays =  Constants.DEFAULT_LOCK_USER_DAYS;
            log.error("getLockUserJobDays error->", e);
        }
        return lockUserDays;
    }
}